﻿//-----------------------------------------------------------------------
// <copyright file="Arguments.cs" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation.
// All rights reserved.
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// This code released under the terms of the 
// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)
// </copyright>
// <author>Yogesh M Joshi</author>
// <email>ymjoshi@live.com</email>
// <date>16-May-2011</date>
//-----------------------------------------------------------------------

namespace SmartTag.Application.CommandLine
{
    using System.Collections.Specialized;
    using System.Text.RegularExpressions;

    /// <summary>
    /// Command line argument parser
    /// </summary>
    public class Arguments
    {
        #region "Private Fields"

        /// <summary>
        /// Paramteters passed through command line
        /// </summary>
        private StringDictionary parameters;

        #endregion

        /// <summary>
        /// Initializes a new instance of the Arguments class
        /// </summary>
        /// <param name="args">Command line arguments</param>
        public Arguments(string[] args)
        {
            this.parameters = new StringDictionary();
            Regex splitParam = new Regex(@"^-{1,2}", RegexOptions.IgnoreCase | RegexOptions.Compiled);
          
            string parameter = null;
            string[] parts;

            foreach (string argument in args)
            {
                // Split the argument
                parts = splitParam.Split(argument, 3);

                switch (parts.Length)
                {
                    case 1:
                        if (parameter != null)
                        {
                            if (this.parameters.ContainsKey(parameter) == false)
                            {
                                this.parameters.Add(parameter, parts[0]);
                            }

                            parameter = null;
                        }

                        break;

                    case 2:

                        if (parameter != null)
                        {
                            if (this.parameters.ContainsKey(parameter) == false)
                            {
                                this.parameters.Add(parameter, "true");
                            }
                        }

                        parameter = parts[1];
                        break;

                    case 3:

                        if (parameter != null)
                        {
                            if (this.parameters.ContainsKey(parameter) == false)
                            {
                                this.parameters.Add(parameter, "true");
                            }
                        }

                        parameter = parts[1];

                        if (this.parameters.ContainsKey(parameter))
                        {
                            this.parameters.Add(parameter, parts[2]);
                        }

                        parameter = null;
                        break;
                }
            }

            if (parameter != null)
            {
                if (this.parameters.ContainsKey(parameter))
                {
                    this.parameters.Add(parameter, "true");
                }
            }
        }

        /// <summary>
        /// Get the parameter
        /// </summary>
        /// <param name="param">Parameter name</param>
        /// <returns>Parameter value</returns>
        public string this[string param]
        {
            get
            {
                return this.parameters[param];
            }
        }
    }
}
